package com.bnyy.medicalHousekeeper.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.SizeUtils;
import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.adapter.ShopServiceGoodsAdapter;
import com.bnyy.medicalHousekeeper.base.BaseActivityImpl;
import com.bnyy.medicalHousekeeper.bean.ServiceGoods;
import com.bnyy.medicalHousekeeper.request.BaseObserverImpl;
import com.bnyy.medicalHousekeeper.request.RequestManager;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.function.Consumer;

import butterknife.BindView;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2022/9/29
 **/
public class ShopServiceGoodsListActivity extends BaseActivityImpl {

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.iv_no_data)
    ImageView ivNoData;
    @BindView(R.id.tv_no_data)
    TextView tvNoData;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;

    private ShopServiceGoodsAdapter adapter;

    public static void show(Context context, int shopId) {
        Intent intent = new Intent(context, ShopServiceGoodsListActivity.class);
        intent.putExtra("shopId", shopId);
        context.startActivity(intent);
    }

    private int shopId;
    private int size = 20;
    private int index = 1;

    @Override
    public OnRefreshListener registerOnRefreshListener() {
        return new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                index = 1;
                getServiceGoods(new Consumer<ArrayList<ServiceGoods>>() {
                    @Override
                    public void accept(ArrayList<ServiceGoods> serviceGoods) {
                        llNoData.setVisibility(serviceGoods.size() == 0 ? View.VISIBLE : View.GONE);
                        adapter.refresh(serviceGoods);
                    }
                });
            }
        };
    }

    @Override
    public OnLoadMoreListener registerOnLoadMoreListener() {
        return new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                index++;
                getServiceGoods(new Consumer<ArrayList<ServiceGoods>>() {
                    @Override
                    public void accept(ArrayList<ServiceGoods> serviceGoods) {
                        adapter.loadMore(serviceGoods);
                    }
                });
            }
        };
    }

    private void getServiceGoods(Consumer<ArrayList<ServiceGoods>> consumer) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("id", shopId);
        params.put("page_index", index);
        params.put("page_size", size);
        requestManager.request(requestManager.mJavaRetrofitService.getShopServiceGoodsList(RequestManager.getJsonRequestBody(params)),
                new BaseObserverImpl<ArrayList<ServiceGoods>>(mRefreshLayout) {
                    @Override
                    public void onSuccess(ArrayList<ServiceGoods> serviceGoods) {
                        super.onSuccess(serviceGoods);
                        mRefreshLayout.setEnableLoadMore(serviceGoods.size() >= size);
                        consumer.accept(serviceGoods);
                    }
                });
    }

    @Override
    public int getLayoutId() {
        return R.layout.layout_recycler_view;
    }

    @Override
    public String getTitleStr() {
        return "全部商品";
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        shopId = getIntent().getIntExtra("shopId", -1);

        adapter = new ShopServiceGoodsAdapter(mContext);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(adapter);
        int padding = SizeUtils.dp2px(12);
        recyclerView.setPadding(padding, padding, padding, padding);
        mRefreshLayout.autoRefresh();
    }
}




























